package simulation;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import simulation.CreatorPackage.Creator;
import simulation.CreatorPackage.centerMassCreator;
import simulation.CreatorPackage.gravityCreator;
import simulation.CreatorPackage.massCreator;
import simulation.CreatorPackage.muscleCreator;
import simulation.CreatorPackage.springCreator;
import simulation.CreatorPackage.viscosityCreator;
import simulation.CreatorPackage.wallRepulsionCreator;
import view.Canvas;


/**
 * XXX
 * 
 * @author Robert C. Duvall
 */
public class Factory {
    // data file keywords
    private static final String MASS_KEYWORD = "mass";
    private static final String SPRING_KEYWORD = "spring";
    private static final String MUSCLE_KEYWORD="muscle";
    private static final String GRAVITY_KEYWORD="gravity";
    private static final String CENTERMASS_KEYWORD="centermass";
    private static final String VISCOSITY_KEYWORD="viscosity";
    private static final String WALLREPULSION_KEYWORD="wall";
    private Canvas myView;

    // mass IDs
    Map<Integer, Mass> myMasses = new HashMap<Integer, Mass>();
    
    private Map<String, Creator> myCreators=new HashMap<String,Creator>();


    /**
     * XXX.
     */
    public Factory(){
    	registerCreators();
    	
    }
    private void registerCreators() {
    	myCreators.put(MASS_KEYWORD, new massCreator(this));
    	myCreators.put(SPRING_KEYWORD, new springCreator(this));
    	myCreators.put(MUSCLE_KEYWORD, new muscleCreator(this));
    	myCreators.put(GRAVITY_KEYWORD, new gravityCreator(this));
    	myCreators.put(CENTERMASS_KEYWORD, new centerMassCreator(this));
    	myCreators.put(VISCOSITY_KEYWORD, new viscosityCreator(this));
    	myCreators.put(WALLREPULSION_KEYWORD, new wallRepulsionCreator(this));
		// TODO Auto-generated method stub
		
	}
	public void loadModel (Model model, File modelFile) {
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    if (myCreators.containsKey(type)){
                    	myCreators.get(type).createObjectInModel(line,model);
                    } 
                }
            }
            input.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    public void setView(Canvas view){
    	myView=view;
    }
    
    public Canvas getView(){
    	return myView;
    }
    public void putMassIntoMyMasses(int id, Mass mass){
    	myMasses.put(id,  mass);
    	
    }
    public Mass getMass(int id){
    	return myMasses.get(id);
    }
}
